package com.taofuxn.yycreazy.ui.fragment;

import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.GifJokAdapter;
import com.taofuxn.yycreazy.mvp.BasePresenter;
import com.taofuxn.yycreazy.mvp.MvpFragment;
import com.taofuxn.yycreazy.mvp.model.NewsertJokBean;
import com.taofuxn.yycreazy.mvp.presenter.RandomPresenter;
import com.taofuxn.yycreazy.mvp.view.IRandomView;
import com.taofuxn.yycreazy.ports.ITaofuAdapterListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * 随机数据
 * Created by wanghuafu on 16/9/17.
 */
public class RandomDataFragment extends MvpFragment implements SwipeRefreshLayout.OnRefreshListener, ITaofuAdapterListener, IRandomView {
    @InjectView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    @InjectView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private ArrayList<NewsertJokBean.NewsertJokResult.NewsertJok> mData;
    private GifJokAdapter mAdapter;
    private RandomPresenter mRandomPresenter;
    private int currpage = 1;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_happy_gif;
    }

    @Override
    protected void init() {
        mData = new ArrayList<>();
        swipeRefreshLayout.setColorSchemeColors(Color.BLACK,Color.RED,Color.BLUE,Color.GREEN);
        swipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new GifJokAdapter(R.layout.item_tab_gif_jok,mData);
        mAdapter.setAdapterListener(this);
        mRecyclerView.setAdapter(mAdapter);

        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        });

    }


    @Override
    protected BasePresenter createDefaultPresenter() {
        mRandomPresenter = new RandomPresenter(this);
        return mRandomPresenter;
    }

    @Override
    public void onRefresh() {
        currpage = 1;
        mRandomPresenter.loadData(true,currpage);
    }

    @Override
    public void dismissLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void requestSuccess(List<NewsertJokBean.NewsertJokResult.NewsertJok> mDatas) {
    }


    @Override
    public void loadMore() {
        Snackbar.make(mRecyclerView,"正在加载...",Snackbar.LENGTH_SHORT).show();
        currpage += 1;
        mRandomPresenter.loadData(false,currpage);
    }

    @Override
    public void loadmoreSuccess(List<NewsertJokBean.NewsertJokResult.NewsertJok> data) {
        mAdapter.addData(data);

    }

    @Override
    public void reflashSuccess(List<NewsertJokBean.NewsertJokResult.NewsertJok> mDatas) {
        mAdapter.setData((ArrayList<NewsertJokBean.NewsertJokResult.NewsertJok>) mDatas);

    }

    @Override
    public String getType() {

        return getArguments().getString("type");
    }
}
